var UserAutoComplete = {
    bind: function (inputId) {
        $("#" + inputId).bsSuggest({
            allowNoKeyword: false, //是否允许无关键字时请求数据
            multiWord: false, //以分隔符号分割的多关键字支持
            getDataMethod: "url", //获取数据的方式，总是从 URL 获取
            effectiveFields: ["userName", "realName", "orgName"],
            effectiveFieldsAlias: {userName: "账号", realName: "真实姓名", orgName: "部门"},
            showHeader: false,
            showBtn: false,
            idField: "userId",
            keyField: "userName",
            url: '/org/user/search?username=', /*优先从url ajax 请求 json 帮助数据，注意最后一个参数为关键字请求参数*/
            processData: function (json) {// url 获取数据时，对数据的处理，作为 getData 的回调函数
                var i, len, data = {value: []};
                if (!json || json.length == 0 || !json.data) {
                    return false;
                }
                len = json.data.length;
                for (i = 0; i < len; i++) {
                    data.value.push({
                        "userId": json.data[i].userId,
                        "userName": json.data[i].userName,
                        "realName": json.data[i].realName,
                        "orgName": json.data[i].orgName
                    });
                }
                return data;
            }
        }).on('onSetSelectValue', function (e, keyword, data) {
            $("#userList").append('<li data-id="' + keyword.id + ':' + keyword.key + '" style="margin-top: 5px;">' +
                '<input class="form-control" value="' + keyword.key + '" type="text" readonly/>' +
                '<a href="javascript:void(0);" onclick="UserAutoComplete.delUser(this);">X</a></li>');
        });
    },

    delUser: function(obj) {
        $(obj).parent().remove();
    },
    getUser: function(){
        var userList = "";
        $("#userList").children().each(function (index, value) {
            userList += value.getAttribute("data-id") + ";";
        });
        return userList;
    }
}
